﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using IRepository;
using Repository;
using IServices;
using Services;

namespace RocketMQHandler
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Init();
            base.OnStartup(e);
        }

        private void ConfigureServices(IServiceCollection service)
        {
            service.TryAddTransient<MainWindow>();
            
            service.AddScoped(serviceProvider =>
            {
                ISqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = "server=127.0.0.1;user id=sa;password=123456;database=kopsoftwms",
                    DbType = SqlSugar.DbType.PostgreSQL,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute,
                    IsShardSameThread = true
                });
                return db;
            });

            service.AddScoped<IMeter_DeviceEnergyDayRepository,Meter_DeviceEnergyDayRepository> ();
            service.AddScoped<IMeter_DeviceEnergyDayServices, Meter_DeviceEnergyDayServices>();

            service.AddScoped<IMeter_DeviceEnergyMonthRepository, Meter_DeviceEnergyMonthRepository>();
            service.AddScoped<IMeter_DeviceEnergyMonthServices, Meter_DeviceEnergyMonthServices>();

            service.AddScoped<IMeter_DeviceEnergyLogRepository, Meter_DeviceEnergyLogRepository>();
            service.AddScoped<IMeter_DeviceEnergyLogService, Meter_DeviceEnergyLogService>();

            service.AddScoped<IMeter_DeviceDataRepository, Meter_DeviceDataRepository>();
            service.AddScoped<IMeter_DeviceDataServices, Meter_DeviceDataServices>();

            service.AddScoped<IMeter_DeviceInfoRepository, Meter_DeviceInfoRepository>();
            service.AddScoped<IMeter_DeviceInfoServices, Meter_DeviceInfoServices>();

            service.AddSingleton<DeviceStatusDataWorker>();
            service.AddSingleton<DeviceEnergyDataWorker>();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            IServiceCollection services = new ServiceCollection();
            ConfigureServices(services);
            services.BuildServiceProvider()
                    .GetRequiredService<MainWindow>()
                    .Show();

        }
    }
}
